import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import signupRouter from '../modules/signup'
import competitionDetails from "../modules/details"
import personalCenterRouter from "../modules/personalCenter"

export const publicRoutes: Array<RouteRecordRaw> = [
  ...signupRouter,
  ...competitionDetails,
  ...personalCenterRouter,
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: '首页',
    meta: {
      title: '大赛平台'
    },
    component: () => import('@/views/homePage/index.vue')
  },
];

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: publicRoutes
})

/* 初始化路由表 */
export function resetRouter() {
	router.getRoutes().forEach((route) => {
		const { name } = route;
		if (name) {
			router.hasRoute(name) && router.removeRoute(name);
		}
	});
}

/**
 * @description 路由拦截 beforeEach
 * */
router.beforeEach(async (to, from, next) => {
  // 动态设置标题
  const title = import.meta.env.VITE_GLOB_APP_TITLE
  document.title = to.meta.title ? (to.meta.title as string) : title
  next()
})

export default router